﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Touch2Like.Model;
using System.Data;

namespace Touch2Like.DAL
{
    public class CouponDesignDAL
    {
        #region Add
        public int Add(CouponDesignInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeOutParam("@ListID",SqlDbType.Int,4),
                db.MakeInParam("@UserGUID",SqlDbType.UniqueIdentifier,16,new Guid(info.UserGUID)),
                db.MakeInParam("@CouponValue",SqlDbType.VarChar,50,info.CouponValue),
                db.MakeInParam("@CouponLocation",SqlDbType.VarChar,200,info.CouponLocation),
                db.MakeInParam("@ExpirationDate",SqlDbType.DateTime,8,info.ExpirationDate)
            };
            try
            {
                db.ExecNonQuery("T2R_CouponDesign_Add", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Update
        public void Update(CouponDesignInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,info.ListID),
                db.MakeInParam("@CouponValue",SqlDbType.VarChar,50,info.CouponValue),
                db.MakeInParam("@CouponLocation",SqlDbType.VarChar,200,info.CouponLocation),
                db.MakeInParam("@ExpirationDate",SqlDbType.DateTime,8,info.ExpirationDate)
            };
            try
            {
                db.ExecNonQuery("T2R_CouponDesign_Update", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Delete
        public void Delete(int listid)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,listid)
            };
            try
            {
                db.ExecNonQuery("T2R_CouponDesign_Delete", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Get By ID
        public CouponDesignInfo GetByID(int listid)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,listid)
            };
            try
            {
                dr = db.RunProc("T2R_CouponDesign_GetByID", param);
                if (dr.Read())
                {
                    CouponDesignInfo info = new CouponDesignInfo();
                    info.ListID = DataHelper.GetInt(dr["ListID"]);
                    info.UserGUID = DataHelper.GetString(dr["UserGUID"]);
                    info.CouponValue = DataHelper.GetString(dr["CouponValue"]);
                    info.CouponLocation = DataHelper.GetString(dr["CouponLocation"]);
                    info.ExpirationDate = DataHelper.GetDateTime(dr["ExpirationDate"]);
                    info.Void = DataHelper.GetInt(dr["Void"]);
                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    return info;
                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        #endregion

        public DataTable GetByUserGUID(string userguid)
        {
            Database db = new Database();

            try
            {
                SqlParameter[] param =
                {
                    db.MakeInParam("@UserGUID", SqlDbType.UniqueIdentifier, 16, new Guid(userguid))
                };
                return db.GetDataSet("T2R_CouponDesign_GetByUserGUID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
    }
}
